package com.jhhc.Weathers.datasupplement.util;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.nio.file.Files;

/**
 * @author LYQ
 * FTP工具类
 */
@Component
public class FTPUtil {

    /**
    *
    * @param ftpPath    FTP文件所在路径
    * @param localPath    本地路径
    * @param fileName     文件名
    */
    public int downloanFtpFlie(FTPClient ftpClient, String ftpPath, String localPath,
                               String fileName){
        int result = -1;
        try {
            ftpClient.enterLocalPassiveMode();
            InputStream inputStream = ftpClient.retrieveFileStream(ftpPath);
            if (inputStream != null) {
                new File(localPath).mkdirs();
                String localFile = localPath + File.separator + fileName;
                File file = new File(localFile);
                if (!file.exists()) {
                    FileOutputStream outputStream = new FileOutputStream(file,true);
                    IOUtils.copy(inputStream, outputStream);
                    outputStream.close();
                    inputStream.close();
                    ftpClient.completePendingCommand();
                    result = 1;
                }else {
                    System.out.println("文件已存在");
                }
            }
            return result;
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("读写异常");
            return result;
        }
    }

    public int fileUpload(FTPClient ftpClient, String ftpPath, String localPath){
        int result = -1;
        try {
            ftpClient.enterLocalPassiveMode();
            File file = new File(localPath);
            InputStream inputStream  = Files.newInputStream(file.toPath());
            ftpClient.storeFile(ftpPath, inputStream);
            inputStream.close();
            result = 1;
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件异常");
            return result;
        }
    }

    /**
     * @param ftpIp   FTP的IP
     * @param ftpUserName  ftp用户名
     * @param ftpPassword  ftp密码
     * @param ftpPort     ftp端口号
     * @return
     */
    public FTPClient getFTPClient(String ftpIp,String ftpUserName, String ftpPassword,int ftpPort){
        FTPClient ftpClient = new FTPClient();
        try{
            ftpClient = new FTPClient();
            ftpClient.connect(ftpIp,ftpPort);
            ftpClient.login(ftpUserName,ftpPassword);
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
                System.out.println("未连接到FTP");
            }else {
                System.out.println("连接成功");
            }
        }catch (SocketException e){
            e.printStackTrace();
            System.out.println("连接下载文件的FTP失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ftpClient;
    }


}
